package com.qf.fmall01.service.impl;

import com.qf.fmall01.dao.ShoppingCartMapper;
import com.qf.fmall01.entity.ShoppingCart;
import com.qf.fmall01.entity.ShoppingCartVO;
import com.qf.fmall01.service.ShoppingCartService;
import com.qf.fmall01.vo.ResStatus;
import com.qf.fmall01.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO addShopCart(ShoppingCart shoppingCart) {

        ResultVO resultVO = new ResultVO();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateStr = simpleDateFormat.format(new Date());
        shoppingCart.setCartTime(dateStr);

        int i = shoppingCartMapper.insert(shoppingCart);

        if (i == 1){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        }else{
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("添加购物车失败");
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listByUserId(Integer userId) {

        ResultVO resultVO = new ResultVO();

        List<ShoppingCartVO> shoppingCartVOList=  shoppingCartMapper.listByUserId(userId);

        if (shoppingCartVOList.size() == 0){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查到购物车信息");
        }else{
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(shoppingCartVOList);
        }

        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO update(Integer cid, Integer cnum) {

        ResultVO resultVO = new ResultVO();

        int i = shoppingCartMapper.updateCnumByCid(cid,cnum);

        if (i == 1){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("更新购物车数量失败");
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listbycids(String cids) {

        ResultVO resultVO = new ResultVO();
        // 处理逗号分隔的 cids ，例如：11,12
        String[] split = cids.split(",");
        List<String> cidsList = Arrays.asList(split);

        List<ShoppingCartVO> shoppingCartVOList= shoppingCartMapper.listbycids(cidsList);

        if (shoppingCartVOList.size() == 0){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查到购物车信息");
        }else{
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(shoppingCartVOList);
        }

        return resultVO;
    }
}
